<?php
/**
 * [weliam] Copyright (c) 2016/3/23
 * success.ctrl
 * 支付成功控制器
 */
defined('IN_IA') or exit('Access Denied');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';

$pagetitle = '支付结果';
wl_load()->func('global');
wl_load()->func('pdo');
wl_load()->func('tpl');
wl_load()->func('message');
wl_load()->func('print');
wl_load()->classs('qrcode');
if ($op == 'display') {
    global $_W, $_GPC;
    $orderno = $_GPC['orderno'];
    $errno = $_GPC['errno'];
    $order = order_get_by_params(" orderno = '{$orderno}' ");
    $createqrcode = new creat_qrcode();
    $createqrcode->creategroupQrcode($order['orderno']);

//    if ($errno == 1){
//        include wl_template('success');
//        exit();
//    }else
    if ($_W['uniacid'] == 4 || $_W['uniacid'] == 6 || $_W['uniacid'] == 13) {
        //获取到orderid 之后换取到商品id
//        $res_sid = pdo_fetchall("SELECT 'sid','starttime' FROM ".tablename('tg_collect'), array('orderno'=>$orderno));
        $order = pdo_fetch("select * from " .tablename('tg_order') ." where orderno = '{$orderno}'");
        if ($order['is_stock'] == 1){
            //TODO 创建日志
            $path = TG_DATA . "/log/" . $_W['uniacid'] . "/order/";
            //首先判断目录存在否
            if (!is_dir($path)) {
                //第3个参数“true”意思是能创建多级目录，iconv防止中文目录乱码
                $res = mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
            }
            $date = date('Y-m-dH', TIMESTAMP);
            file_put_contents($path . $date . ".log", var_export(
                    array(
                        'ip' => CLIENT_IP,
                        'op' => "订单重复",
                        'uniacid' => $_W['uniacid'],
                        'filepath' => __FILE__,
                        'orderno' => $orderno,
                        'order' => $order,
                        'line' => __LINE__,
                        'time' => date('Y-m-d H:i:s', TIMESTAMP)
                    ),
                    true) . PHP_EOL, FILE_APPEND);
            include wl_template('success');
            exit();
        }
        $user = pdo_fetch("select * from " . tablename("tg_collect") . " where orderno = '" . $orderno . "'");
        $sid = $user['sid'];

        $starttime = $user['starttime'];
        //商品表减去库存
        $res_goods = pdo_fetch("SELECT * FROM " . tablename('tg_goods') . " where id = '" . $sid . "'");
        //拿出来商品的库存
        $kucun = $res_goods['week_quantum'];
//        var_dump($res_goods);die();
        $goods_kucun = unserialize($kucun);//商品库存

//        计算今天是周几
        $da = date("w", $starttime);
//        var_dump($starttime);die();
        if ($da == 1) {
            $week_str = 1;
        } else if ($da == 2) {
            $week_str = 2;
        } else if ($da == 3) {
            $week_str = 3;
        } else if ($da == 4) {
            $week_str = 4;
        } else if ($da == 5) {
            $week_str = 5;
        } else if ($da == 6) {
            $week_str = 6;
        } else if ($da == 0) {
            $week_str = 7;
        }
        foreach ($goods_kucun as &$val) {
            if ($val['week'] == $week_str) {

                $start = strval(date('H:i', $starttime));
                $start = strtotime($start);
                $start_time = strtotime($val['start_time']);
                if ($start == $start_time) {
                    $kucun = $val['kucun'];
                    if ($kucun > 0) {
                        $val['kucun']--;

//                        //TODO 创建日志
                        $path = TG_DATA . "/log/" . $_W['uniacid'] . "/order/";
                        //首先判断目录存在否
                        if (!is_dir($path)) {
                            //第3个参数“true”意思是能创建多级目录，iconv防止中文目录乱码
                            $res = mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
                        }
                        $date = date('Y-m-dH', TIMESTAMP);
                        file_put_contents($path . $date . ".log", var_export(
                                array(
                                    'ip' => CLIENT_IP,
                                    'op' => "订单成功",
                                    'uniacid' => $_W['uniacid'],
                                    'filepath' => __FILE__,
                                    'kucun' => $kucun,
                                    'kucun2' => $val['kucun'],
                                    'user' => $user,
                                    'orderno' => $orderno,
                                    'line' => __LINE__,
                                    'time' => date('Y-m-d H:i:s', TIMESTAMP)
                                ),
                                true) . PHP_EOL, FILE_APPEND);
                        if ($order['pay_price'] == 0) {
                            $url = app_url('order/order/detail', array('id' => $order['id']));
                            pay_onesuccess($order['orderno'], $order['openid'], '', TIMESTAMP, $url);
                        }
                    }else{
//                        if ($_W['uniacid'] == 4){
                        if ($order['pay_price'] == 0) {
                            pdo_update('tg_order' , array('status' => 9) , array('id' => $order['id']));
                        }else {
                            pdo_update('tg_order', array('status' => 10), array('id' => $order['id']));
                        }
                            $errno = 2;
                            include wl_template('success');
                            exit();
//                        }

                    }
                }
            }
        }

        $goods_kucun = serialize($goods_kucun);
        $res_up['week_quantum'] = $goods_kucun;
//        $res_up["id"] = $res_sid;
        $res = pdo_update('tg_goods', array('week_quantum' => $goods_kucun), array('id' => $sid));
        pdo_update('tg_order', array('is_stock' => 1), array('id' => $order['id']));
        if ($res) {
            include wl_template('success');
            exit();
        }
    }
//    //TODO 创建日志
//    $path = TG_DATA . "/log/" . $_W['uniacid'] . "/order/";
//    //首先判断目录存在否
//    if (!is_dir($path)) {
//        //第3个参数“true”意思是能创建多级目录，iconv防止中文目录乱码
//        $res = mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
//    }
//    $date = date('Y-m-d', TIMESTAMP);
//    file_put_contents($path . $date . ".log", var_export(
//            array(
//                'ip' => CLIENT_IP,
//                'op' => "订单成功结束",
//                'uniacid' => $_W['uniacid'],
//                'filepath' => __FILE__,
//                'orderno' => $orderno,
//                'line' => __LINE__,
//                'time' => date('Y-m-d H:i:s', TIMESTAMP)
//            ),
//            true) . PHP_EOL, FILE_APPEND);
    include wl_template('success');
}
exit();
